import re
from lbuffermod import lbuffer
startdelim = '## '
enddelim = '##END\n'

class arb(object):
    def __init__(self):     
        return
    def parse(self,ifile):
        linerbuffer = lbuffer(ifile[0]).linebuffer
        names = ['']
        ptr,idx = 0,0
#        print "Linerbuffer Length: " + str(len(linerbuffer))
        while(ptr < len(linerbuffer) - 1):
            if not (re.match(enddelim,linerbuffer[ptr])):
                names[idx] += linerbuffer[ptr]
#                print "Appended name [" + str(ptr) + "] :: " + linerbuffer[ptr] + " idx[" + str(idx) + "]"
                ptr = ptr + 1
#               print "Appended name [" + str(ptr) + "] :: " + linerbuffer[ptr] + " idx[" + str(idx) + "]"
#            print "PTR is: " + str(ptr)
#            print "LPTR +1 contains :: " + linerbuffer[ptr + 1]
#            print "LBPtR [" + str(idx) + "] contains: " + linerbuffer[ptr]
            if (re.match(enddelim,linerbuffer[ptr])):
                names.append('')
                idx = 1 + idx
                ptr = ptr + 1
              
        idx = 0
        strippednames = ['']
        while(idx < len(names) -1):
             strippednames[idx] = re.sub('##END$','',names[idx]).lstrip().rstrip()
             strippednames.append('')
#             print "stripped index: " + str(idx)
             idx = idx + 1
#             print "stripped index incremented to: " + str(idx) 
        return strippednames
